<!--
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 -->

<script>
import config from "./config.js";
import {getPhoneInfo} from "./utils/getPhoneInfo.js";

// App.vue
export default {
    onLaunch(options) {
        console.log("[App]App Launch", options);
        //页面显示重置vuex开发环境监听vuex状态变化
        /*    process.env.SAAS_ENV == "dev"
            ? this.$watch(
                  "$store.state",
                  (newVal, oldVal) => {
                      uni.setStorageSync("VUEX-STATE", newVal);
                      uni.setStorageSync("VUEX-YZHVIDEO", newVal.yzhvideo);
                  },
                  { deep: true }
              )
            : ""; */
        const {query} = options; // query参数
        const {path} = options; // 页面路径
        // 缓存页面参数
        uni.setStorageSync("currentOption", options);
        // 缓存当前页面路径
        const pathAndQuery = this.pathByQuery(path, query);
        // 缓存当前路径
        uni.setStorageSync("currentPage", pathAndQuery);
        uni.setStorageSync("appCode", process.env['APP_CODE'])
        // #ifdef MP
        console.log(uni.getAccountInfoSync());
        config.appid = uni.getAccountInfoSync().miniProgram.appId;
        uni.setStorageSync("appid", config.appid);
        if (uni.getExtConfig) {
            // 获取提交版本号
            uni.getExtConfig({
                success(res) {
                    if (res.extConfig.audit_version) {
                        uni.setStorageSync("auditVersion", res.extConfig.audit_version);
                    }
                },
            });
        }
        // #endif

        // 获取手机信息
        getPhoneInfo();

        this.getNavHeight(); //保存自定义导航栏高度
        // #ifdef MP
        // 小程序初始化配置
        this.checkMPApplyUpdate();
        // #endif
    },
    onShow(options) {
        console.log("[App]App Show", options);
        //缓存页面场景值
        uni.setStorageSync("scene", options.scene);
    },
    methods: {
        getNavHeight() {
            // 获取胶囊高度，配置导航高度
            let CustomBarHeight;
            let StatusBarHeight;
            const systemInfo = uni.getSystemInfoSync();

            CustomBarHeight = systemInfo.platform === "android" ? systemInfo.statusBarHeight + 50 : systemInfo.statusBarHeight + 45;
            StatusBarHeight = systemInfo.statusBarHeight;
            // #ifdef MP-ALIPAY
            CustomBarHeight = systemInfo.statusBarHeight;
            StatusBarHeight = systemInfo.statusBarHeight + systemInfo.titleBarHeight;
            // #endif
            uni.setStorageSync("navHeight", CustomBarHeight + "px");
            uni.setStorageSync("statusBarHeight", StatusBarHeight + "px");
        },
        //检查是否有新版本
        checkMPApplyUpdate() {
            const updateManager = uni.getUpdateManager();
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                const {hasUpdate} = res;
                console.log("[小程序是否有新版本]", hasUpdate);
            });
            updateManager.onUpdateReady(function () {
                uni.showModal({
                    title: "更新提示",
                    content: "新版本已经准备好，请重启小程序",
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate();
                        }
                    },
                });
            });
            updateManager.onUpdateFailed(function () {
                // 新的版本下载失败
            });
        },
        pathByQuery(path, query) {
            let pathQuery = "";
            if (!this.$u.isEmpty(query)) {
                pathQuery = "?";
            }
            for (let i in query) {
                pathQuery += `&${i}=${query[i]}`;
            }
            return `/${path}${pathQuery}`;
        },
    },
};
</script>

<style lang="scss">
@import "@/components/uview-ui/index.scss";
/* 引入项目iconfont图标 */
@import "./common/css/icon.css";
//全局公共样式
@import "./common/css/common.scss";
//修改全局组件样式
@import "./common/css/global.scss";
// 自定义css
@import "./common/css/custom.scss";
//animate动画
@import "./common/css/animate.css";
/*每个页面公共css */
@import "@/components/active-form/active-form.scss";//注入active-form样式

</style>
